package com.cliff.joinus.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.androidquery.AQuery;
import com.cliff.joinus.Constants;
import com.cliff.joinus.JoinusControlPoint;
import com.cliff.joinus.R;
import com.cliff.joinus.RendererMediator;
import com.cliff.joinus.RendererMediator.MEDIATOR_OPERATION;
import com.cliff.joinus.RendererMediator.MEDIATOR_STATUS;

public class PlayControlActivity extends SherlockActivity implements RendererMediator.OnRendererStatusChangedListener, OnClickListener, OnSeekBarChangeListener{
	
	protected final String TAG = "PlayControlActivity";

	protected final int[][] ACTIONBARMENU = new int[][]{Constants.AB_BACK, Constants.AB_STOP_BACK};
	
	protected RendererMediator mediator = null;
	protected AQuery aq = null;
	
	protected final int MESSAGE_PLAYPAUSE = 1;
	protected final int MESSAGE_NEXT = 2;
	protected final int MESSAGE_PREV = 3;
	protected final int MESSAGE_NOWPLAYING = 4;
	protected final int MESSAGE_STATUS = 5;
	protected final int MESSAGE_DURATION = 6;
	protected final int MESSAGE_CURRENT = 7;

	protected Handler mHandler = null; 
	protected class UpdateDisplayHandler extends Handler{          
        public void handleMessage(Message msg) {  
            switch (msg.what) {  
            case MESSAGE_PLAYPAUSE:  
        		aq.id(R.id.buttonPlayPause).clickable(msg.arg1==1);		
        		aq.id(R.id.buttonPlayPause).image((msg.arg2==1)?android.R.drawable.ic_media_play:android.R.drawable.ic_media_pause);		
                break;  
            case MESSAGE_NEXT: 
        		aq.id(R.id.buttonNext).clickable(msg.arg1==1);		
        		break;
            case MESSAGE_PREV:  
        		aq.id(R.id.buttonPrevious).clickable(msg.arg1==1);		
        		break;
            case MESSAGE_NOWPLAYING:  
    			aq.id(R.id.textViewNowPlaying).text((String)msg.obj);
        		break;
            case MESSAGE_STATUS:
    			Toast.makeText(PlayControlActivity.this, "status" + msg.obj, Toast.LENGTH_SHORT).show();
        		break;
            case MESSAGE_DURATION:
    			aq.id(R.id.textViewTotal).text((String)msg.obj);
        		break;
            case MESSAGE_CURRENT:
    			aq.id(R.id.textViewCurrent).text((String)msg.obj);
        		break;
            }  
        };  
    };  	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_playcontrol);
		super.onCreate(savedInstanceState);
		mHandler = new UpdateDisplayHandler(); 

		aq = new AQuery(this);
		aq.id(R.id.buttonNext).clicked(this);
		aq.id(R.id.buttonPlayPause).clicked(this);
		aq.id(R.id.buttonPrevious).clicked(this);
		aq.id(R.id.seekBar).getSeekBar().setOnSeekBarChangeListener(this);

		JoinusControlPoint cp = JoinusControlPoint.getInstance();
		setTitle(cp.getSelectedRenderer().getFriendlyName());

		mediator = cp.getSelectedRendererMediator();
		mediator.setListener(this);		

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		mediator.setListener(null);
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		
		MainActivity.initMenu(menu, ACTIONBARMENU);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (android.R.id.home == item.getItemId()) {
			finish();
			return true;
		}
		
		if (Constants.GROUP_COMMON == item.getGroupId()) {
			switch(item.getItemId()) {
			case Constants.ITEM_BACK:
				finish();
				break;
			case Constants.ITEM_STOP_BACK:
				mediator.operate(MEDIATOR_OPERATION.STOP, 0);
				finish();
			}
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		switch(id) {
		case R.id.buttonNext:
			mediator.operate(MEDIATOR_OPERATION.NEXT, RendererMediator.CURRENT_POSITION);
			break;
		case R.id.buttonPlayPause:
			mediator.operate(MEDIATOR_OPERATION.PLAYPAUSE, RendererMediator.CURRENT_POSITION);
			break;
		case R.id.buttonPrevious:
			mediator.operate(MEDIATOR_OPERATION.PREV, RendererMediator.CURRENT_POSITION);
			break;
		}
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub		
		Log.d(TAG, "onProgressChanged:progress="+progress+":fromUser="+fromUser);
		if (!fromUser)
			return;

	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		Log.d(TAG, "onStartTrackingTouch()");		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		Log.d(TAG, "onStopTrackingTouch()");		
		mediator.operate(MEDIATOR_OPERATION.SEEK, seekBar.getProgress());		
	}

	@Override
	public void updatePlayPauseButton(boolean enabled, boolean isPlay) {
		// TODO Auto-generated method stub
		mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_PLAYPAUSE, enabled?1:0, isPlay?1:0));
	}

	@Override
	public void updateNextButton(boolean enabled) {
		// TODO Auto-generated method stub
		mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_NEXT, enabled?1:0, 0));
	}

	@Override
	public void updatePrevButton(boolean enabled) {
		// TODO Auto-generated method stub
		mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_PREV, enabled?1:0, 0));
	}

	@Override
	public void updateTitle(String title) {
		// TODO Auto-generated method stub
		if (null != title)
			mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_NOWPLAYING, title));
	}

	@Override
	public void updateStatus(MEDIATOR_STATUS status) {
		// TODO Auto-generated method stub
		if (null != status) {
			mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_STATUS, status.toString()));
//			Toast.makeText(this, "status="+status, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void updateDuration(int maxProgress, String duration, String current) {
		// TODO Auto-generated method stub
		if (maxProgress >= 0)
			aq.id(R.id.seekBar).getSeekBar().setMax(maxProgress);
		if (null != duration)
			mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DURATION, duration));
		if (null != current)
			mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_CURRENT, current));
	}

	@Override
	public void updateProgress(int progress) {
		// TODO Auto-generated method stub
		aq.id(R.id.seekBar).getSeekBar().setProgress(progress);		
	}

	@Override
	public void updateSeekBar(boolean enabled) {
		// TODO Auto-generated method stub
		aq.id(R.id.seekBar).getSeekBar().setClickable(enabled);
	}

}
